
package todolist.util;

import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * General utilities. All methods here are static.
 * @author Ivan
 */
public class Utils {
	
	private static final String IMAGE_DIRECTORY = "images" + getFileSeparator();
	
	/**
	 * Creates and returns an image based on given file name.
	 * GUI prompt will already be shown on creation failure.
	 */
	public static ImageIcon createImageIcon(String pstrName, Component pobjMessageDialogParentComponent) {
		String strPath = IMAGE_DIRECTORY + pstrName;
		try {
			return new ImageIcon(ImageIO.read(new File(strPath)));
		} catch (IOException objEx) {
			JOptionPane.showMessageDialog(
					pobjMessageDialogParentComponent, "Couldn't find file: " + strPath, "File Not Found", // parent, message, title, messageType, icon
				JOptionPane.WARNING_MESSAGE, null);
			return null;
		}
	}
	
	/**
	 * Determine file/directory-separator, independent of OS used,
	 *	typically forward slash on Unix, backslash on Windows.
	 */
	public static String getFileSeparator() {
		return System.getProperty("file.separator");
	}
	
	/**
	 * Determine line-separator, independent of OS used, typically backslash n.
	 */
	public static String getLineSeparator() {
		return System.getProperty("line.separator");
	}
	
	/**
	 * Determine application root directory by system property user.dir.
	 */
	public static String getApplicationPath() {
		return System.getProperty("user.dir");
	}
	
	/**
	 * Determine by system property os.name whether app is running on Windows OS.
	 */
	public static boolean isWindowsOS() {
		String strOS = System.getProperty("os.name");
		if (strOS == null)
			return false;
		return strOS.matches("Windows.*");
	}
	
	/**
	 * Determine by system property os.arch whether app is running on a 64-bit OS.
	 */
	public static boolean is64BitOS() {
		String strArch = System.getProperty("os.arch");
		if (strArch == null)
			return false;
		return strArch.contains("64");
	}
	
	/**
	 * Returns resulting String after removing N words from Input, or empty String if too many words removed.
	 * Input must not be null, and N has to be positive.
	 * Word is defined by one or more non-whitespace characters.
	 */
	public static String discardNWordsFromString(String pstrInput, int pintN) {
		// parameter validation
		if (pstrInput == null)
			throw new IllegalArgumentException("Utils.discardNWordsFromString(): Input must not be null.");
		if (pintN < 0)
			throw new IllegalArgumentException("Utils.discardNWordsFromString(): Number of words to discard must be positive.");
		String strWords = "\\s*";
		while (pintN-- > 0)
			strWords += "(?:\\S+\\s+)";
		Pattern objPattern = Pattern.compile(strWords + "(.*)", Pattern.CASE_INSENSITIVE);
		Matcher objMatcher = objPattern.matcher(pstrInput);
		if (!objMatcher.matches())
			return "";
		return objMatcher.group(1);
	}
	
}
